﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;
using Premiere.Framework;
using System.Data;

namespace PremiereMX
{
    public class PartLogic
    {
        IList m_partgroupList;
        IList m_currencyList;
        IList m_unitList;
        IList m_partCategoryList1;
        IList m_partCategoryList2;
        IList m_partCategoryList3;
        IList m_vendorList;
        IList m_partServiceList;

        FieldInfo m_currentFieldInfo;
        IList m_fieldInfoList;
  
        PartDTO m_subject = new PartDTO();
        IMainForm m_mainForm;
        IUserControlPart m_form;
        EditMode m_editMode = EditMode.OK;
        public PartLogic(IMainForm mainForm, IUserControlPart form)
        {
            m_mainForm = mainForm;
            m_form = form;
        }
        public void Init()
        {
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
            //updatePartList();
            loadList();
        }
        private void loadList()
        {
            m_partgroupList = ServiceRegistry.GetInstance().GetPartGroupService().FindPartGroupByGroupingType(GroupingType.Detail);
            m_currencyList = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();
            m_unitList = ServiceRegistry.GetInstance().GetUnitService().LoadAll();
            m_partCategoryList1 = ServiceRegistry.GetInstance().GetPartCategoryService().FindAllPartCategory1();
            m_partCategoryList2 = ServiceRegistry.GetInstance().GetPartCategoryService().FindAllPartCategory2();
            m_partCategoryList3 = ServiceRegistry.GetInstance().GetPartCategoryService().FindAllPartCategory3();
            ArrayList customerList = (ArrayList)ServiceRegistry.GetInstance().GetCustomerService().LoadAll();
            ArrayList supplierList = (ArrayList)ServiceRegistry.GetInstance().GetSupplierService().LoadAll();
            customerList.AddRange(supplierList);
            m_vendorList = customerList;
            m_partServiceList = ServiceRegistry.GetInstance().GetPartService().FindPartByPartType(PartType.Material);
            m_form.ClearActivityList();
            if (m_subject.Id > 0)
            {
                IList activities = ServiceRegistry.GetInstance().GetPartService().FindStockCardEntryInfoPart(m_subject.Id);
                foreach (StockCardEntryInfoDTO activity in activities)
                {
                    m_form.AddActivity(activity);
                }
                StockCardInfo sci = (StockCardInfo)ServiceRegistry.GetInstance().GetPartService().FindTotalStockCardInfoPart(m_subject.Id);
                m_form.CurrentStock = sci == null ? 0 : sci.Balance;
            }
            m_fieldInfoList = ServiceRegistry.GetInstance().GetPartService().GetFieldInfos();
        }
        private void updateForm()
        {
            m_form.Code = m_subject.Code;
            m_form.Named = m_subject.Name;
            m_form.LastUpdate = m_subject.LastUpdate;
            m_form.ComputerName = m_subject.ComputerName;
            m_form.UserUpdateBy = m_subject.UserName;
            m_form.Barcode = m_subject.Barcode;
            m_form.PartGroup = m_subject.PartGroup;
            m_form.PartType = m_subject.PartType;
            m_form.Unit = m_subject.Unit;
            m_form.DecimalPlace = m_subject.DecimalPlace;
            m_form.Currency = m_subject.Currency;
            m_form.CostMethod = m_subject.CostMethod;
            m_form.PartCategory1 = m_subject.PartCategory1;
            m_form.PartCategory2 = m_subject.PartCategory2;
            m_form.PartCategory3 = m_subject.PartCategory3;
            m_form.MaximumStock = m_subject.MaximumStock;
            m_form.MinimumStock = m_subject.MinimumStock;
            m_form.CostPrice = m_subject.CostPrice;
            m_form.SellPrice = m_subject.SellPrice;
            m_form.Taxable = m_subject.Taxable;
            m_form.Multiuom = m_subject.MultiUom;
            m_form.CurrentStock = m_subject.CurrentStock;
            m_form.Discontinue = m_subject.Discontinue;
            m_form.Note = m_subject.Note;
            StockCardInfo sci = (StockCardInfo)ServiceRegistry.GetInstance().GetPartService().FindTotalStockCardInfoPart(m_subject.Id);
            m_form.CurrentStock = sci == null ? 0 : sci.Balance;
            m_form.ClearUomList();
            m_form.ClearVendorPartNameList();
            m_form.ClearServicerPartList();
            m_form.ClearActivityList();
            foreach (object key in m_subject.UomList.Keys)
            {
                UnitConversionDTO ecvs = (UnitConversionDTO)m_subject.UomList[key];
                m_form.AddUOM(ecvs);
            }
            foreach (object key in m_subject.VendorPartNameList.Keys)
            {
                VendorPartNameDTO ve = (VendorPartNameDTO)m_subject.VendorPartNameList[key];
                m_form.AddVendorPartName(ve);
            }
            foreach (object key in m_subject.ServicePartList.Keys)
            {
                ServicePartDTO sp = (ServicePartDTO)m_subject.ServicePartList[key];
                m_form.AddServicePart(sp);
            }
            IList activities = ServiceRegistry.GetInstance().GetPartService().FindStockCardEntryInfoPart(m_subject.Id);
            foreach (StockCardEntryInfoDTO activity in activities)
            {
                m_form.AddActivity(activity);
            }
        }
        public IList GetVendorList()
        {
            return m_vendorList;
        }
        public IList GetPartServiceList()
        {
            return m_partServiceList;
        }
        public IList PartGroupList
        {
            get { return m_partgroupList; }
        }
        public IList CurrencyList
        {
            get { return m_currencyList; }
        }
        public IList UnitList
        {
            get {
                return  m_unitList;
            }
        }

        public IList PartCategoryList1
        {
            get { return m_partCategoryList1; }
        }
        public IList PartCategoryList2
        {
            get { return m_partCategoryList2; }
        }
        public IList PartCategoryList3
        {
            get { return m_partCategoryList3; }
        }
        public IList FieldList
        {
            get { return m_fieldInfoList; }
        }
        private void SetButtonEditMode(EditMode editMode)
        {
            m_form.SetButtonEditMode(editMode);
            m_editMode = editMode;
        }
        private void updatePartList()
        {
            IList list = ServiceRegistry.GetInstance().GetPartService().LoadAll();
            m_form.UpdateList(list);
        }
        public void Edit()
        {
            m_form.SetToReadOnly(false);
            m_form.SetButtonEditMode(EditMode.Insert);
        }
        private void updateSubject()
        {
            m_subject.Code = m_form.Code;
            m_subject.ComputerName = Environment.MachineName;
            m_subject.Name = m_form.Named;
            m_subject.Note = string.Empty;
            m_subject.UserName = m_mainForm.GetUser().Code;       
            m_subject.LastUpdate = DateTime.Now;
            m_subject.Barcode = m_form.Barcode;
            m_subject.PartGroup = m_form.PartGroup;
            m_subject.PartType = m_form.PartType;
            m_subject.Unit = m_form.Unit;
            m_subject.DecimalPlace = m_form.DecimalPlace;
            m_subject.Currency = m_form.Currency;
            m_subject.CostMethod = m_form.CostMethod;
            m_subject.PartCategory1 = m_form.PartCategory1;
            m_subject.PartCategory2 = m_form.PartCategory2;
            m_subject.PartCategory3 = m_form.PartCategory3;
            m_subject.MaximumStock = m_form.MaximumStock;
            m_subject.MinimumStock = m_form.MinimumStock;
            m_subject.CostPrice = m_form.CostPrice;
            m_subject.SellPrice = m_form.SellPrice;
            m_subject.Taxable = m_form.Taxable;
            m_subject.MultiUom = m_form.Multiuom;
            m_subject.Discontinue = m_form.Discontinue;
            m_subject.Note = m_form.Note;
            m_subject.DTOToString = m_mainForm.GetGeneralSetup().PartToString;
            m_subject.ClearUomList();
            m_subject.ClearVendorPartNameList();
            m_subject.ClearServicePartList();
            IList uoms = m_form.GetListUom();
            IList vendorListName = m_form.GetListVendorPartName();
            IList servicePartList = m_form.GetListServicePart();
            foreach (UnitConversionDTO unitcvs in uoms)
            {
                m_subject.AddUnitConversion(unitcvs);
            }
            foreach (VendorPartNameDTO vdr in vendorListName)
            {
                m_subject.AddVendorPartName(vdr);
            }
            if (m_subject.PartType.Equals(PartType.Service))
            {
                foreach (ServicePartDTO sp in servicePartList)
                {
                    m_subject.AddServicePartName(sp);
                }
            }
        }
        internal void UpdateSubject(PartDTO curr)
        {
            curr = (PartDTO)ServiceRegistry.GetInstance().GetPartService().Get(curr.Id);
            m_subject = curr;
            m_form.ClearForm();
            updateForm();
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
        }
        public void Save()
        {
            try
            {
                if (m_form.IsValidSubject())
                {
                    updateSubject();
                    m_subject.Id = ServiceRegistry.GetInstance().GetPartService().Save(m_subject);
                    m_form.ShowMessage("Record has been saved!");
                    m_form.SetToReadOnly(true);
                    SetButtonEditMode(EditMode.OK);
                    m_form.UpdateList(new object[] { m_subject });
                }
            }
            catch (Exception ex)
            {
                m_form.ShowMessage(ex.Message);
            }
        }
        internal void Delete()
        {
            if (m_subject.Id == 0) return;
            ServiceRegistry.GetInstance().GetPartService().Delete(m_subject);
            m_form.RemoveItem(m_subject.Id);
            m_form.ClearForm();
        }
        internal void New()
        {
            m_subject = new PartDTO();
            m_form.ClearForm();
            SetButtonEditMode(EditMode.Insert);
            m_form.SetToReadOnly(false);
            m_form.FocusToCode();
        }
        public void Refresh()
        {
        }
        internal void Print()
        {
            DataSet ds = ServiceRegistry.GetInstance().GetPartService().GetPreviewReportData(m_subject.Id);
            m_mainForm.Print(ds);
        }
        public void AddNewUnitConversion()
        {
            m_form.AddUOM(new UnitConversionDTO());
        }
        public void Find()
        {
            m_form.ClearRecordList();
            //FieldInfo fi = m_form.FieldInfo;
            //m_currentFieldInfo = fi;
            //if (fi == null)
            //    throw new Exception("Field Not Set");
            //fi.Value = m_form.Filter;
            //fi.ASC = m_form.Ascending;
            ArrayList allresult = new ArrayList();
            foreach (FieldInfo f in this.FieldList)
            {
                f.Value = m_form.Filter;
                IList result = ServiceRegistry.GetInstance().GetPartService().FindByField(f);
                allresult.AddRange(result);
            }
            //IList result = ServiceRegistry.GetInstance().GetPartService().FindByField(fi);
            m_form.UpdateList(allresult);
            m_form.FoundRecord = "Found " + allresult.Count.ToString() + " item(s)";
        }

        internal PartDTO GetSubject()
        {
            return m_subject;
        }

        internal void UpdateSubject()
        {
            updateSubject();
            m_subject.Id = ServiceRegistry.GetInstance().GetPartService().Save(m_subject);
        }

        internal void LoadAllPart()
        {
            m_form.ClearRecordList();
            (FieldList[0] as FieldInfo).Value = "";
            IList allresult = ServiceRegistry.GetInstance().GetPartService().FindByField(FieldList[0] as FieldInfo);
            m_form.UpdateList(allresult);
            m_form.FoundRecord = "Found " + allresult.Count.ToString() + " item(s)";
        }
    }
}
